﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MockObject
{
    public class Analyze
    {
        private IWebService service ;
        private IEmailService email;

        public IWebService Service
        {
            get { return service; }
            set { service = value; }
        }

        public IEmailService Email
        {
            get { return email; }
            set { email = value; }
        }
        //public Analyze(IWebService m_service)
        //{
        //    service = m_service;
        //}

        public void Test(String fileName)
        {
            if (fileName.Length < 8) // Nếu filename nhỏ hơn 8
            {
                try
                {
                    //throw exception
                    service.LogError("Filename to short: " + fileName);
                }
                catch (Exception e)
                {
                    //
                    email.SendEmail("a", "subject", e.Message);
                }
            }
            
        }
    }

    //Khởi tạo WebService interface
    public interface IWebService
    {
        void LogError(String Message);
    }

    //Khởi tạo EmailService interface
    public interface IEmailService
    {
        void SendEmail(string to, string subject, string body);
    }


    //Stub object cho Web service to throw Exception when needed
    public class StubService : IWebService
    {

        public Exception ToThrow;
        public void LogError(String message)
        {
            if (ToThrow != null)
            {
                throw ToThrow;
            }
        }
    }

    // Tao mock object cho email service
    public class MockEmailService : IEmailService
    {
        public string To;
        public string Subject;
        public string Body;

        public void SendEmail(string to, string subject, string body)
        {
            To = to;
            Subject = subject;
            Body = body;
        }
    }
}
